﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Connection;
using System.Collections;
using System.Xml;

/***************************************
 * Class Name:  IMSResponse
 * Author:      Chen Xin
 * Version:     1.0
 * Date:        2006-10-19
 * Description: ArcIMS 响应信息
 ***************************************/

/// <summary>
/// 对 ArcIMS 应用服务器发送请求
/// 所获得的服务器的响应信息
/// </summary>
public class IMSResponse
{
	public IMSResponse()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    //
    // 获得服务的基本信息
    //
    //public BaseClass.MapExtent GetServiceInfo(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    //{
    //    BaseClass.MapExtent mapExtent = new BaseClass.MapExtent();

    //    System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
    //    axlResponse.LoadXml(serverConnection.Send(sAxlText));
    //    System.Xml.XmlNodeList nodeEnvelope = axlResponse.GetElementsByTagName("ENVELOPE");

    //    foreach (System.Xml.XmlNode xmlNode in nodeEnvelope) 
    //    {
    //        if (xmlNode.ParentNode.Name == "PROPERTIES" && xmlNode.Attributes["name"].InnerXml == "Initial_Extent")
    //        {
    //            // 分别得到地图各边界值
    //            mapExtent.Left = double.Parse(xmlNode.Attributes["minx"].InnerXml);
    //            mapExtent.Top = double.Parse(xmlNode.Attributes["miny"].InnerXml);
    //            mapExtent.Right = double.Parse(xmlNode.Attributes["maxx"].InnerXml);
    //            mapExtent.Bottom = double.Parse(xmlNode.Attributes["maxy"].InnerXml);
    //        }
    //    }
    //    // 返回地图范围
    //    return mapExtent;
    //}

    public string GetServiceInfoResponse(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    {
        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();

        return serverConnection.Send(sAxlText);
    }

    //
    // 获得地图或图例地址
    //
    public string GetImageResponse(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection, string sFlag)
    {
        string sImageUrl;

        // 声明一个 XML 的服务响应对象，即 ArcXML
        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText));

        if (sFlag == "map" && axlResponse.GetElementsByTagName("OUTPUT").Count == 1)
        {
            System.Xml.XmlNodeList xmlNodeList = axlResponse.GetElementsByTagName("OUTPUT");
            // 获取这个节点的 url 属性值
            sImageUrl = xmlNodeList[0].Attributes["url"].Value;
        }
        else if (sFlag == "legend" && axlResponse.GetElementsByTagName("LEGEND").Count == 1)
        {
            System.Xml.XmlNodeList xmlNodeList = axlResponse.GetElementsByTagName("LEGEND");
            // 获取这个节点的 url 属性值
            sImageUrl = xmlNodeList[0].Attributes["url"].Value;
        } 
        else 
        {
            sImageUrl = "";
        }

        return sImageUrl;
    }

    public string[] GetImagePrintResponse(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    {
        string[] sImageUrl = new string[2];
        string sMapUrl;
        string sLegendUrl;

        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText));

        if (axlResponse.GetElementsByTagName("OUTPUT").Count == 1)
        {
            System.Xml.XmlNodeList nodeOutput = axlResponse.GetElementsByTagName("OUTPUT");
            sMapUrl = nodeOutput[0].Attributes["url"].Value;
            sImageUrl[0] = sMapUrl;
        }
        if (axlResponse.GetElementsByTagName("LEGEND").Count == 1)
        {
            System.Xml.XmlNodeList nodeLegend = axlResponse.GetElementsByTagName("LEGEND");
            sLegendUrl = nodeLegend[0].Attributes["url"].Value;
            sImageUrl[1] = sLegendUrl;
        }

        return sImageUrl;
    }

    //
    // 鼠标指定烟田地块的 FIELD_ID
    //
    public string GetFeatureResponseBySpatialQuery(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    {
        string sFieldId = "";

        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText,"Query"));

        System.Xml.XmlElement root = axlResponse.DocumentElement;
        System.Xml.XmlNodeList myNodeList = root.GetElementsByTagName("FIELDS");

        if (myNodeList.Count > 0)
        {
            System.Xml.XmlNode xn = myNodeList.Item(0);
            sFieldId = xn.Attributes["FIELD_ID"].InnerXml;
        }

        return sFieldId;

    }

    /// <summary>
    /// 返回鼠标指定位置的feature的基本信息
    /// Author: Yang
    /// Date: 2006-11-20
    /// Description: 第1、3、5、6类型的feature图层的信息直接由SpatialQuery得到，
    /// 而2、4、7、8类型的feature图层的信息需要由SpatialQuery得到的ID，然后通过Utility类中的getFeatureInfoByEID()的得到。
    /// </summary>
    /// <param name="sAxlText">GET_FEATURES 的 ArcIMS 请求</param>
    /// <param name="serverConnection">ArcIMS 应用服务器连接</param>
    /// <param name="featureLayerType">查询要素图层的类型</param>
    ///                                 1 == 烟田（年度种植）
    ///                                 2 == 规划
    ///                                 3 == 轮作
    ///                                 4 == 自然灾害
    ///                                 5 == 适宜区
    ///                                 6 == 耕地
    ///                                 7 == 水窖信息
    ///                                 8 == 烤房信息
    /// <param name="year">业务年度</param>
    /// <returns>返回鼠标指定位置的feature的基本信息（XML文件格式）</returns>
    public string GetFeatureInfoBySpatialQuery(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection, string featureLayerType, string year, string[] sCoordinate)
    {
        Hashtable hashTable = new Hashtable();
        hashTable.Add("281", "洪涝灾");
        hashTable.Add("282", "风灾");
        hashTable.Add("283", "旱灾");
        hashTable.Add("284", "冰雹灾");
        hashTable.Add("119", "水池");
        hashTable.Add("120", "水窖");
        hashTable.Add("10069", "立式炉");
        hashTable.Add("10070", "平板炉");
        hashTable.Add("10071", "云红-96");
        hashTable.Add("10072", "老式炉");
        hashTable.Add("10289", "YM-A型");
        hashTable.Add("10290", "YM-B型");
        hashTable.Add("10291", "YQⅢ型");
        hashTable.Add("10292", "YM-C");
        hashTable.Add("10293", "YZ-A");

        string xmlResult = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        xmlResult += "<FeatureInfo>" + "\n";

        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText, "Query"));

        System.Xml.XmlElement root = axlResponse.DocumentElement;
        System.Xml.XmlNodeList myNodeList = root.GetElementsByTagName("FIELDS");

        if (myNodeList.Count > 0)
        {
            string strName = "";
            System.Xml.XmlNodeList myNodeList2 = myNodeList.Item(0).ChildNodes;//.GetElementsByTagName("FIELD");

            foreach (System.Xml.XmlNode xn in myNodeList2)
            {
                strName = xn.Attributes["name"].InnerXml;
                string[] atts = strName.Split('.');
                strName = atts[atts.Length - 1];

                switch (featureLayerType)
                {
                    //if (strName == "AREA")
                    //{
                    //    xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                    //}
                    //xmlResult += "<ACCOUNT_YEAR>" + year + "</ACCOUNT_YEAR>\n";
                    case "烟田":
                        if (strName == "EID" || strName == "AREA" || strName == "ADDRESS" || strName == "ACCOUNT_YEAR")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;

                    case "规划":
                        if (strName == "TID" || strName == "AREA" || strName == "FIELD_LOCALITY" || strName == "PLANTER_AMOUNT" || strName == "ACCOUNT_YEAR" || strName == "FIELD_ID")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "轮作":
                        if (strName == "EID" || strName == "AREA" || strName == "ACCOUNT_YEAR")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "灾害":
                        if (strName == "TID" || strName == "NATURAL_TIME" || strName == "ACCOUNT_YEAR"
                             || strName == "NATURAL_LOCAL" || strName == "AREA" || strName == "NATURAL_RATE" || strName == "FIELD_ID")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        if (strName == "NATURAL_TYPE")
                        {
                            foreach (DictionaryEntry de in hashTable)
                            {
                                if ((string)de.Key == xn.Attributes["value"].InnerXml)
                                    xmlResult += "<" + strName + ">" + de.Value + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "适宜区":
                        if (strName == "EID" || strName == "AREA")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "耕地":
                        if (strName == "EID" || strName == "AREA")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "水窖水池":
                        if (strName == "WELL_NUMBER" || strName == "WELL_CUBAGE" || strName == "IRRAGATE_AREA"
                            || strName == "PLANTER_NAME" || strName == "PLANTER_NO" || strName == "PLANTER_ADDRESS")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        if (strName == "WELL_TYPE")
                        {
                            foreach (DictionaryEntry de in hashTable)
                            {
                                if ((string)de.Key == xn.Attributes["value"].InnerXml)
                                    xmlResult += "<" + strName + ">" + de.Value + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "烤房":
                        if (strName == "OVEN_NUMBER" || strName == "OVEN_TYPE" || strName == "OVEN_AREA"
                            || strName == "OVEN_CAPACITY" || strName == "PLANTER_NAME" || strName == "PLANTER_ADDRESS" || strName == "FIELD_ID")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        if (strName == "OVEN_TYPE")
                        {
                            foreach (DictionaryEntry de in hashTable)
                            {
                                if ((string)de.Key == xn.Attributes["value"].InnerXml)
                                    xmlResult += "<" + strName + ">" + de.Value + "</" + strName + ">\n";
                            }
                        }
                        break;
                    case "管网沟渠":
                        //if (strName == "CANAL_NUMBER" || strName == "LEN" || strName == "TOWN_ID" || strName=="ACCOUNT_YEAR")
                        if (strName == "CANAL_NUMB" || strName == "LEN" || strName == "TOWN_ID" || strName == "ACCOUNT_YE")
                        {
                            if (xn.Attributes["value"].InnerXml.Length == 0)
                            {
                                xmlResult += "<" + strName + ">" + "没有录入数据" + "</" + strName + ">\n";
                            }
                            else
                            {
                                xmlResult += "<" + strName + ">" + xn.Attributes["value"].InnerXml + "</" + strName + ">\n";
                            }
                        }
                        break;

                }
            }
        }
        xmlResult += "<PointX>" + sCoordinate[0] + "</PointX>\n";
        xmlResult += "<PointY>" + sCoordinate[1] + "</PointY>\n";
        xmlResult += "</FeatureInfo>";
        return xmlResult;
    }
}
